import pygame
from settings import *

def draw_text(surface, text, position, color, font, shadow=False, shadow_color=(0, 0, 0), center=False):
    font_obj = pygame.font.Font(font, 36)
    text_obj = font_obj.render(text, True, color)

    if shadow:
        shadow_obj = font_obj.render(text, True, shadow_color)
        shadow_pos = (position[0] + 2, position[1] + 2)
        if center:
            shadow_rect = shadow_obj.get_rect(center=shadow_pos)
            surface.blit(shadow_obj, shadow_rect)
        else:
            surface.blit(shadow_obj, shadow_pos)

    if center:
        text_rect = text_obj.get_rect(center=position)
        surface.blit(text_obj, text_rect)
    else:
        surface.blit(text_obj, position)


def button(surface, x, label, click_sound=None, y_offset=0):
    # Define button properties
    button_width = 200
    button_height = 50
    button_color = (0, 0, 255)
    text_color = (255, 255, 255)

    mouse_pos = pygame.mouse.get_pos()
    mouse_click = pygame.mouse.get_pressed()

    button_rect = pygame.Rect(x, SCREEN_HEIGHT//2 + y_offset, button_width, button_height)

    # Draw button
    pygame.draw.rect(surface, button_color, button_rect, border_radius=10)
    draw_text(surface, label, (x + button_width//2, SCREEN_HEIGHT//2 + 25 + y_offset),
             text_color, FONTS["medium"], shadow=True, center=True)

    # Button interaction
    if button_rect.collidepoint(mouse_pos):
        pygame.draw.rect(surface, (0, 100, 255), button_rect, border_radius=10)
        if mouse_click[0]:
            if click_sound:
                click_sound.play()
            return True
    return False